package com.duruikang.LeaveAPI.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duruikang.LeaveAPI.common.sercurity.utils.JwtTokenUtil;
import com.duruikang.LeaveAPI.dao.ExamineDao;
import com.duruikang.LeaveAPI.entity.Examine;
import com.duruikang.LeaveAPI.entity.Teacher;
import com.duruikang.LeaveAPI.service.ExamineService;
import com.duruikang.LeaveAPI.service.TeacherInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author szh Drik
 */
@Service
public class ExamineServiceImpl extends ServiceImpl<ExamineDao, Examine> implements ExamineService {

    @Autowired
    TeacherInformationService teacherService;

    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @Override
    public boolean submitExamination(String leavingId, String token, int examineResult, String examineComment) {
        boolean res = false;
        Examine newExamine = new Examine();
        newExamine.setExamineId(UUID.randomUUID().toString());
        newExamine.setLeavingId(leavingId);

        //查找并设置教师id
        String accountId = jwtTokenUtil.getUsername(token);
        Teacher teacher = teacherService.query().eq("account_id", accountId).one();
        if (teacher == null) {
            return false;
        }
        newExamine.setTeacherId(teacher.getTeacherId());

        newExamine.setExamineDate(LocalDate.now());//使用当前时间
        newExamine.setExamineResult(examineResult);
        newExamine.setExamineComment(examineComment);
        res = save(newExamine);
        return res;
    }

    @Override
    public List<Map<String, String>> getHistory(String token) {
        String accountId = jwtTokenUtil.getUsername(token);
        Teacher teacher = teacherService.query().eq("account_id", accountId).one();
        if (teacher == null) {
            return null;
        }
        return baseMapper.getSimpleHistory(teacher.getTeacherId());
    }

    @Override
    public List<Map<String, String>> getExamines(String leavingId) {
        return baseMapper.getExamineById(leavingId);
    }
}
